package com.zhao.fishingguideserver.controller;

import com.zhao.fishingguideserver.POJO.Post;
import com.zhao.fishingguideserver.POJO.Result;
import com.zhao.fishingguideserver.mapper.PostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理端帖子管理控制器
 */
@RestController
@RequestMapping("/admin/post")
public class AdminPostController {
    
    @Autowired
    private PostMapper postMapper;
    
    /**
     * 获取帖子列表（包含所有状态）
     */
    @GetMapping("/list")
    public Result<java.util.Map<String, Object>> getPostList(@RequestParam(required = false) String keyword,
                                                             @RequestParam(required = false) String status,
                                                             @RequestParam(required = false) Integer page,
                                                             @RequestParam(required = false) Integer pageSize) {
        try {
            int safePage = (page == null || page < 1) ? 1 : page;
            int safePageSize = (pageSize == null || pageSize < 1 || pageSize > 100) ? 10 : pageSize;
            int offset = (safePage - 1) * safePageSize;
            
            int total = postMapper.countPosts(status, keyword);
            java.util.List<Post> list = postMapper.selectPostsPaged(status, keyword, offset, safePageSize);
            
            java.util.Map<String, Object> data = new java.util.HashMap<>();
            data.put("list", list);
            data.put("total", total);
            data.put("page", safePage);
            data.put("pageSize", safePageSize);
            return Result.success(data);
        } catch (Exception e) {
            return Result.error("获取帖子列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取帖子详情
     */
    @GetMapping("/detail/{id}")
    public Result<Post> getPostDetail(@PathVariable Integer id) {
        try {
            Post post = postMapper.selectById(id);
            if (post == null) {
                return Result.error("帖子不存在");
            }
            return Result.success(post);
        } catch (Exception e) {
            return Result.error("获取帖子详情失败：" + e.getMessage());
        }
    }
    
    /**
     * 更新帖子
     */
    @PutMapping("/update")
    public Result<Void> updatePost(@RequestBody Post post) {
        try {
            int result = postMapper.updatePost(post);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("更新帖子失败");
            }
        } catch (Exception e) {
            return Result.error("更新帖子失败：" + e.getMessage());
        }
    }
    
    /**
     * 删除帖子
     */
    @DeleteMapping("/delete/{id}")
    public Result<Void> deletePost(@PathVariable Integer id) {
        try {
            // 软删除：修改状态为deleted
            Post post = new Post();
            post.setId(id);
            post.setStatus("deleted");
            int result = postMapper.updatePost(post);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("删除帖子失败");
            }
        } catch (Exception e) {
            return Result.error("删除帖子失败：" + e.getMessage());
        }
    }
    
    /**
     * 批量删除帖子
     */
    @PostMapping("/batchDelete")
    public Result<Void> batchDeletePosts(@RequestBody List<Integer> ids) {
        try {
            int successCount = 0;
            for (Integer id : ids) {
                Post post = new Post();
                post.setId(id);
                post.setStatus("deleted");
                int result = postMapper.updatePost(post);
                if (result > 0) {
                    successCount++;
                }
            }
            return Result.success();
        } catch (Exception e) {
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }
    
    /**
     * 置顶/取消置顶帖子
     */
    @PutMapping("/setTop/{id}")
    public Result<Void> setTop(@PathVariable Integer id,
                               @RequestParam Boolean isTop) {
        try {
            Post post = new Post();
            post.setId(id);
            post.setIsTop(isTop);
            int result = postMapper.updatePost(post);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("操作失败");
            }
        } catch (Exception e) {
            return Result.error("操作失败：" + e.getMessage());
        }
    }
    
    /**
     * 设为热门/取消热门
     */
    @PutMapping("/setHot/{id}")
    public Result<Void> setHot(@PathVariable Integer id,
                               @RequestParam Boolean isHot) {
        try {
            Post post = new Post();
            post.setId(id);
            post.setIsHot(isHot);
            int result = postMapper.updatePost(post);
            if (result > 0) {
                return Result.success();
            } else {
                return Result.error("操作失败");
            }
        } catch (Exception e) {
            return Result.error("操作失败：" + e.getMessage());
        }
    }
}

